using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Runtime.Serialization;

[Serializable]
public class ShoppingCart: DataTable
{
    protected ShoppingCart(SerializationInfo info, StreamingContext context) : base(info, context) { }
    public ShoppingCart()
    {
        /*
        this.ID = a;
        this.ten = b;
        this.don_gia = c;
        this.so_luong = d;
        this.thanh_tien = e;*/
        
        this.Columns.Add("IDTaiLieu", typeof(int)).DefaultValue = -1;
        this.Columns.Add("TenTL", typeof(string)).DefaultValue = "";
        this.Columns.Add("DonGia", typeof(int));
        this.Columns.Add("SoLuong", typeof(int));
        this.Columns.Add("ThanhTien", typeof(long));
        //this.Columns.Add("NhaCungCap", typeof(int));
        //this.Columns.Add("Gia", typeof(double)).DefaultValue = 0;
        //this.Columns.Add("SoLuong", typeof(int)).DefaultValue = 1;
        //this.Columns.Add("Discount", typeof(double)).DefaultValue = 0;
        //this.Columns.Add("SoTien", typeof(double), "Gia*SoLuong");
        //this.Columns.Add("LOAITL", typeof(LOAITL));

        this.PrimaryKey = new DataColumn[] { this.Columns["IDTaiLieu"] };
        
        
        
    }

    public void Add(int ID,string ten,int dongia, int SoLuong ,long thanh_tien)
    {
        this.AddProduct(ID,ten,dongia,SoLuong ,thanh_tien, false);
    }

    public void Update(int MaTL, int SoLuong)
    {
        //this.AddProduct(MaTL, SoLuong, true);
    }

    public void Remove(int MaTL)
    {
       
        try
        {
            DataRow Product = this.Rows.Find(MaTL);
            this.Rows.Remove(Product);
        }
        catch
        {
            Console.WriteLine("Product not found !");
        }
    }
    
    public long Total
    {
        get
        {
            object value = this.Compute("SUM(ThanhTien)", "");
            return value == DBNull.Value ? 0 : (long)value;
        }
    }

    public int Count_Product
    {
        get
        {
            object value = this.Compute("SUM(SoLuong)", "");
            try
            {
                return System.Int32.Parse(value.ToString());
            }
            catch (Exception e)
            {
                return 0;
            }
            //return value == DBNull.Value ? 0 : (int)value;
           // return this.Rows.Count;
        }
    }

    private void AddProduct(int ID, string ten, int dongia, int SoLuong, long thanh_tien, bool Update)
    {
        try
        {
            DataRow Item = this.Rows.Find(ID);
            Item["SoLuong"] = SoLuong + (Update ? 0 : (int)Item["SoLuong"]);
            Item["ThanhTien"] = thanh_tien + (Update ? 0 : (long)Item["ThanhTien"]);
        }
        catch
        {
            DataRow newRow = this.NewRow();
            newRow["IDTaiLieu"] = ID;
            newRow["TenTL"] = ten;
            newRow["DonGia"] = dongia;
            newRow["SoLuong"] = SoLuong;
            newRow["ThanhTien"] = thanh_tien;
            this.Rows.Add(newRow);
            //String sql = "SELECT MaTL, TenTL, Gia, " + SoLuong + " as SoLuong FROM TAILIEU WHERE MaTL=@MaTL";
            //Database.Fill(this, sql, "@MaTL", ProductID);
        }
    }
}
